package cn.com.allunion.sms.service.mybatis.impl;

import cn.com.allunion.common.DateModule;
import cn.com.allunion.common.DateUtils;
import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.mapper.SmstimerMapper;
import cn.com.allunion.sms.pojo.Smstimer;
import cn.com.allunion.sms.pojo.SmstimerExample;
import cn.com.allunion.sms.service.mybatis.SmstimerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定时发送短信定时器业务逻辑接口实现类
 * Created by YangJie on 2016/4/18.
 */
@Service
@Transactional
public class SmstimerServiceImpl implements SmstimerService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SmstimerServiceImpl.class) ;

    @Autowired
    SmstimerMapper smstimerMapper ;

    @Autowired
    DateModule dateModule;

    @Override
    public boolean save(Smstimer smstimer) {
        if (null == smstimer) {
            LOGGER.warn("the smstimer object is null.");
            return false ;
        }
        int result = smstimerMapper.insert(smstimer) ;
        if (result > 0) {
            return true ;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Smstimer> findByJobName(String jobName) {
        if (StringUtils.isBlank(jobName)) {
            LOGGER.warn("the jobName object is null.");
            return null ;
        }
        SmstimerExample example = new SmstimerExample();
        SmstimerExample.Criteria criteria = example.createCriteria();
        criteria.andJobNameEqualTo(jobName) ;
        List<Smstimer> smstimers = smstimerMapper.selectByExample(example);
        return smstimers ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Smstimer> findByCriteria(String userId, String createStartTime,
                                  String createEndTime, String timingStartTime, String timingEndTime,
                                  String receiveNum, String smsContext, String state) {

        QueryResult<Smstimer> queryResult = findByCriteria(userId, createStartTime, createEndTime,
                timingStartTime, timingEndTime, receiveNum, smsContext, state, -1, -1) ;

        return queryResult.getResultList() ;
    }
    public QueryResult<Smstimer> findByCriteria(String userId, String createStartTime, String createEndTime,
                                                String timingStartTime, String timingEndTime, String receiveNum,
                                                String smsContext, String state, int pageCurrent, int pageSize) {
        SmstimerExample example = new SmstimerExample();
        SmstimerExample.Criteria criteria = example.createCriteria();
        criteria.andUserBaseEqualTo(userId);
        if (StringUtils.isNotBlank(createStartTime) && StringUtils.isNotBlank(createEndTime)) {
            criteria.andCreateDateBetween(dateModule.getDayBegin(DateUtils.convertToDate(createStartTime, "yyyyMMddHHmmss")),
                    dateModule.getDayEnd(DateUtils.convertToDate(createEndTime,"yyyyMMddHHmmss"))) ;
        }

        if (StringUtils.isNotBlank(timingStartTime) && StringUtils.isNotBlank(timingEndTime)) {
            criteria.andTimingDateBetween(dateModule.getDayBegin(DateUtils.convertToDate(timingStartTime, "yyyyMMddHHmmss")),
                    dateModule.getDayEnd(DateUtils.convertToDate(timingEndTime,"yyyyMMddHHmmss")));
        }

        if (StringUtils.isNotBlank(receiveNum)) {
            criteria.andReceiveNumLike("%"+receiveNum+"%") ;
        }
        if (StringUtils.isNotBlank(smsContext)) {
            criteria.andSmsContextLike("%"+smsContext+"%");
        }
        if (StringUtils.isNotBlank(state)) {
            criteria.andStateEqualTo(state) ;
        }


        if (-1 != pageCurrent && -1 != pageSize) {
            PageHelper.startPage(pageCurrent, pageSize);
        }

        List<Smstimer> smstimers = smstimerMapper.selectByExample(example);

        QueryResult<Smstimer> queryResult = null ;
        if (-1 != pageCurrent && -1 != pageSize) {
            PageInfo<Smstimer> pageInfo = new PageInfo<>(smstimers);
            queryResult = new QueryResult<>(smstimers, pageInfo.getTotal());
        } else {
            queryResult = new QueryResult<>(smstimers, smstimers.size()) ;
        }

        return queryResult ;
    }

    @Override
    public boolean update(Smstimer smstimer) {
        if (null == smstimer) {
            LOGGER.warn("the smstimer object is null.");
            return false ;
        }
        int result = smstimerMapper.updateByPrimaryKey(smstimer) ;
        if (result > 0) {
            return true ;
        }
        return false;
    }

    @Override
    public boolean deleteById(String timerId) {
        if (StringUtils.isBlank(timerId)) {
            LOGGER.warn("the timerId object is null.");
            return false ;
        }
        int result = smstimerMapper.deleteByPrimaryKey(timerId) ;
        if (result > 0) {
            return true ;
        }
        return false;
    }
}
